package com.example.weight.greenSeasons.flocculation;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.example.weight.navigation.SijiqingFlocculationNavActivity;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class  xuningyuebaoyang_nav extends AppCompatActivity {
    private String name, role, factory;

    private Button toWeiliaobeng1, toWeiliaobeng2, toTuoshui1, toTuoshui2,toTuoshui3, toChuchou;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuningyuebaoyang_nav);

        Intent previousIntent = getIntent();
        name = previousIntent.getStringExtra("name");
        role = previousIntent.getStringExtra("role");
        factory = previousIntent.getStringExtra("factory");

        TitleBar titleBar = findViewById(R.id.xuning_month_nav_titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {}

            @Override
            public void onRightClick(View v) {}
        });

        toWeiliaobeng1 = findViewById(R.id.xuning_month_nav_weiliaobeng1);
        toWeiliaobeng2 = findViewById(R.id.xuning_month_nav_weiliaobeng2);
        toTuoshui1 = findViewById(R.id.xuning_month_nav_tuoshui1);
        toTuoshui2 = findViewById(R.id.xuning_month_nav_tuoshui2);
        toTuoshui3 = findViewById(R.id.xuning_month_nav_tuoshui3);
        toChuchou = findViewById(R.id.xuning_month_nav_chuchou);

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + "/sjq/flocculationProduceMaintenanceMonth/is_lastweek_complete";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
            try {
                Log.d("nav - response",response);
                JSONArray data = new JSONArray(response);
//                JSONObject jsonObject = new JSONObject(response);
                if (data.length() > 0) {
//                    JSONArray data = jsonObject.getJSONArray("data");
                    int checkedColor = Color.parseColor("#66CC66");
                    int halfCheckedColor = Color.parseColor("#FF9966");
                    int i = 0;
                    if (data.getInt(i++) ==2)
                        toWeiliaobeng1.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        toWeiliaobeng1.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) ==2)
                        toWeiliaobeng2.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        toWeiliaobeng2.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) ==2)
                        toTuoshui1.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        toTuoshui1.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) ==2)
                        toTuoshui2.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        toTuoshui2.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) ==2)
                        toTuoshui3.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        toTuoshui3.setBackgroundColor(halfCheckedColor);
                    if (data.getInt(i++) ==2)
                        toChuchou.setBackgroundColor(checkedColor);
                    else if (data.getInt(i-1) == 1)
                        toChuchou.setBackgroundColor(halfCheckedColor);

//                    if (data.getString(i++).equals("true"))
//                        toWeiliaobeng1.setBackgroundColor(checkedColor);
//                    if (data.getString(i++).equals("true"))
//                        toWeiliaobeng2.setBackgroundColor(checkedColor);
//                    if (data.getString(i++).equals("true"))
//                        toTuoshui1.setBackgroundColor(checkedColor);
//                    if (data.getString(i++).equals("true"))
//                        toTuoshui2.setBackgroundColor(checkedColor);
//                    if (data.getString(i++).equals("true"))
//                        toTuoshui3.setBackgroundColor(checkedColor);
//                    if (data.getString(i++).equals("true"))
//                        toChuchou.setBackgroundColor(checkedColor);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }, error -> Toast.makeText(xuningyuebaoyang_nav.this,
                "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show()) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new HashMap<>();
                map.put("equipmentsName", "喂料泵1_喂料泵2_1#絮凝脱水系统_2#絮凝脱水系统_" +
                        "3#絮凝脱水系统_除臭系统");
                map.put("menstanceNum", "4_4_4_4_4_3");
                map.put("role", role.equals("操作人")?"operator":"examiner");
                map.put("factory", factory);
                return map;
            }
        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            boolean needRefresh = data.getBooleanExtra("needRefresh", true);
            if (needRefresh)
                refresh();
        }
    }

    private void go(String fName) {
        Intent intent =new Intent(xuningyuebaoyang_nav.this,
                xuningyuebaoyang.class);
        intent.putExtra("name", name);
        intent.putExtra("role", role);
        intent.putExtra("fName", fName);
        intent.putExtra("factory", factory);
        startActivityForResult(intent, 1);
    }

    private void goChecker(String fName) {
        Intent intent =new Intent(xuningyuebaoyang_nav.this,
                xuningyuebaoyangChecker.class);
        intent.putExtra("name", name);
        intent.putExtra("role", role);
        intent.putExtra("fName", fName);
        intent.putExtra("factory", factory);
        startActivityForResult(intent, 1);
    }

    public void toWeiliaobeng1(View view){
        if(role.equals("检查人")){
            goChecker("喂料泵1");
        }
        else {
            go("喂料泵1");
        }
    }
    public void toWeiliaobeng2(View view){
        if(role.equals("检查人")){
            goChecker("喂料泵2");
        }
        else {
            go("喂料泵2");
        }
    }
    public void toTuoshui1(View view){
        if(role.equals("检查人")){
            goChecker("1#絮凝脱水系统");
        }
        else {
            go("1#絮凝脱水系统");
        }
    }
    public void toTuoshui2(View view){
        if(role.equals("检查人")){
            goChecker("2#絮凝脱水系统");
        }
        else {
            go("2#絮凝脱水系统");
        }
    }
    public void toTuoshui3(View view){
        if(role.equals("检查人")){
            goChecker("3#絮凝脱水系统");
        }
        else {
            go("3#絮凝脱水系统");
        }
    }
    public void toChuchou(View view){
        if(role.equals("检查人")){
            goChecker("除臭系统");
        }
        else {
            go("除臭系统");
        }
    }

    // 刷新activity
    public void refresh() {
        finish();
        Intent intent = new Intent(xuningyuebaoyang_nav.this,
                xuningyuebaoyang_nav.class);
        intent.putExtra("name", name);
        intent.putExtra("role", role);
        intent.putExtra("factory", factory);
        startActivity(intent);
    }
}